package com.vpa.graphics;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Paint;
import android.graphics.Rect;


public class DigitalGauge extends Spot
{
	private double value;
	public String units;
	private Texture gauge;
	private Rect r = new Rect();
	private static int[][] digi_positions = new int[][]{{10, 130}, {110, 65}, {10, 65}};
	private int smallTextSize = 16;
	private GLText smText;
	Paint tp, tlp;
    private boolean newt = true;
	
	public DigitalGauge(Texture gaugebun, String title, String units)
	{
		gauge = gaugebun;
		drawables = new Texture[1];
		drawables[0] = gauge;
		this.title = title.substring(0, (title.length() >= 10)?(10):(title.length()));
		this.units = units.substring(0, (units.length() >= 10)?(10):(units.length()));
		owidth = gaugebun.res.width;
		oheight = gaugebun.res.height;
		setDims(owidth, oheight, false);
		originalAspRat = owidth/(float)oheight;
		setDims(owidth, oheight, false);
	    x = 400;
		readyToLoad = true;
	}
	
	public void setValue(double val)
	{
		value = val;
	}
	
	protected Texture getTexture()
	{
		return gauge;
	}
	public void draw(GL10 gl, GLView v)
	{
		if(newt || text == null || tp == null)
		{
			TPC ot;
			if((ot = GraphicsController.fontCache.get("DS-DIGIB.TTF" + text_size)) != null)
			{
				text = ot.text;
				tp = ot.p;
			}
			else
			{
				text = new GLText(gl, v.c.getAssets());
				tp = text.load("DS-DIGIB.TTF", text_size, 4, 4).p;
				GraphicsController.fontCache.put("DS-DIGIB.TTF" + text_size, new TPC(text, tp));
			}
			
			if((ot = GraphicsController.fontCache.get("DS-DIGIB.TTF" + smallTextSize)) != null)
			{
				smText = ot.text;
				tlp = ot.p;
			}
			else
			{
				smText = new GLText(gl, v.c.getAssets());
				tlp = smText.load("DS-DIGIB.TTF", smallTextSize, 4, 4).p;
				GraphicsController.fontCache.put("DS-DIGIB.TTF" + smallTextSize, new TPC(smText, tlp));
			}
			newt = false;
		}
		gauge.draw(gl);
		gl.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA );
		String gen = GraphicsController.doubleTrunc(value, 2);
		gen = gen.substring(0, (gen.length() >= 9)?(9):(gen.length()));
		tp.getTextBounds(gen, 0, gen.length(), r);
		drawText(0f, 1f, 0f, 1f, gen, gauge.vertices[6] - (getWidth()/(float)owidth)*digi_positions[0][0] - r.width(),
				gauge.vertices[4] - (getHeight()/(float)oheight)*digi_positions[0][1], text);
		drawText(0f, 1f, 0f, 1f, title, gauge.vertices[6] - (getWidth()/(float)owidth)*digi_positions[1][0],
				gauge.vertices[4] - (getHeight()/(float)oheight)*digi_positions[1][1], smText);
		tlp.getTextBounds(units, 0, units.length(), r);
		drawText(0f, 1f, 0f, 1f, units, gauge.vertices[6] - (getWidth()/(float)owidth)*digi_positions[2][0] - r.width(),
				gauge.vertices[4] - (getHeight()/(float)oheight)*digi_positions[2][1], smText);
    	gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}
	
	public void changeTextSize(int s, int sms)
	{
		if(text_size != s)
		{
			text_size = s;
			smallTextSize = sms;
			newt = true;
		}
	}
	
	protected void setDims(int w, int h, boolean b)
	{
		super.setDims(w, h, b);
		changeTextSize((int)(80/(float)200*h), (int)(16/(float)200*h));
	}
}
